//
//  SHD_TingTableViewCell.m
//  B段项目
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 谢美娜. All rights reserved.
//

#import "SHD_TingTableViewCell.h"

@implementation SHD_TingTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _midImageView = [[UIImageView alloc]init];
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.textColor = [UIColor grayColor];
        _titleLabel = [[myTitleLabel alloc]init];
        _likeLabel = [[UILabel alloc]init];
        _likeLabel.textColor = [UIColor grayColor];
        _unameLabel = [[UILabel alloc]init];
        _unameLabel.textColor = [UIColor grayColor];
        _playImageView = [[UIImageView alloc]init];
        [self.contentView addSubview:_midImageView];
        [self.contentView addSubview:_nameLabel];
        [self.contentView addSubview:_titleLabel];
        [self.contentView addSubview:_likeLabel];
        [self.contentView addSubview:_unameLabel];
        [self.contentView addSubview:_playImageView];
        
        
    }
    return self;
}
-(void)layoutSubviews
{
    [super layoutSubviews];

    if ([[[SHD_music palyMusic] musicString] isEqualToString:[_tingList.playInfo objectForKey:@"musicUrl"]]) {
        
        
        _playImageView.image = [UIImage imageNamed:@"GZzanting"];
//        [[SHD_music palyMusic]setMusicUrl:[_tingList.playInfo objectForKey:@"musicUrl"] plagFlag:_flag];
        
        //_flag += 1;
    }
    else
    {
        _playImageView.image = [UIImage imageNamed:@"GZbofang"];
//        [[SHD_music palyMusic]setMusicUrl:[_tingList.playInfo objectForKey:@"musicUrl"] plagFlag:_flag];
       // _flag = 0;
    }

    _nameLabel.frame = CGRectMake(15 * FitWidth, 15 * FitHeight, 100 * FitWidth, 10 * FitHeight);
    [_nameLabel NightWithType:UIViewColorTypeNormal];
    _midImageView.frame = CGRectMake(_nameLabel.frame.origin.x + 30 * FitWidth, _nameLabel.frame.origin.y + 20 * FitHeight + _nameLabel.frame.size.height, 285 * FitWidth, 170 * FitHeight);
    _titleLabel.frame = CGRectMake(_midImageView.frame.origin.x, _midImageView.frame.origin.y + _midImageView.frame.size.height + 20 * FitHeight, 285 *FitWidth, 45 * FitHeight);
    _unameLabel.frame = CGRectMake(_titleLabel.frame.origin.x, _titleLabel.frame.size.height + _titleLabel.frame.origin.y + 10 * FitHeight, 285 * FitWidth, 10 * FitHeight);
    _likeLabel.frame = CGRectMake(260 * FitWidth, _unameLabel.frame.origin.y + _unameLabel.frame.size.height + 10 * FitHeight, 100 * FitWidth, 10 * FitHeight);
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(savePhoto:)];
    longPress.minimumPressDuration = 1;
    [_midImageView addGestureRecognizer:longPress];
    _midImageView.userInteractionEnabled = YES;
    _playImageView.frame = CGRectMake(_midImageView.frame.origin.x + 122 * FitWidth, _midImageView.frame.origin.y + 150 * FitHeight, 41 * FitWidth, 41 * FitWidth);
    _playImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self  action:@selector(playTing)];
    [_playImageView addGestureRecognizer:tap];
    
    _likeLabel.textAlignment = 2;
    _titleLabel.numberOfLines = 0;
    _unameLabel.numberOfLines = 1;
    _unameLabel.textAlignment = 1;
    _titleLabel.textAlignment = 1;
    _nameLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:10 * FitWidth];
    _unameLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:10 * FitWidth];
    _likeLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:10 * FitWidth];

    
}
#pragma mark 图片保存

-(void)savePhoto:(UILongPressGestureRecognizer *)gesture
{

    
    if (gesture.state == UIGestureRecognizerStateBegan) {
        UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                      initWithTitle:@"提示"
                                      delegate:self
                                      cancelButtonTitle:@"取消"
                                      destructiveButtonTitle:@"保存到相册"
                                      otherButtonTitles: nil];
        actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
        [actionSheet showInView:self.contentView];
        
    }
}
#pragma mark 保存相片方法
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        UIImageWriteToSavedPhotosAlbum(_midImageView.image, self, NULL, NULL);
        
    }
}
#pragma mark 音乐播放
-(void)playTing
{
    if (_flag == 0) {
        
        [[SHD_music palyMusic]setFlag:_flag];
        _playImageView.image = [UIImage imageNamed:@"GZzanting"];
        [[SHD_music palyMusic]setMusicUrl:[_tingList.playInfo objectForKey:@"musicUrl"] plagFlag:_flag];
        [[SHD_music palyMusic]setMusicString:[_tingList.playInfo objectForKey:@"musicUrl"]];
        _flag += 1;
    }
    else
    {
        [[SHD_music palyMusic]setFlag:_flag];
         _playImageView.image = [UIImage imageNamed:@"GZbofang"];
        [[SHD_music palyMusic]setMusicUrl:[_tingList.playInfo objectForKey:@"musicUrl"] plagFlag:_flag];
        _flag = 0;
    }
    
}
-(void)setTingList:(SHD_TingList *)tingList
{
    _tingList = tingList;
    
    _nameLabel.text = [NSString stringWithFormat:@"%@ · %@", tingList.Name, tingList.enname];
    [_midImageView sd_setImageWithURL:[NSURL URLWithString:tingList.coverimg] placeholderImage:[UIImage imageNamed:@"SHD_Scroll"]];
    _titleLabel.text = tingList.title;
    _unameLabel.text = [NSString stringWithFormat:@"by:%@", [tingList.userinfo objectForKey:@"uname"]];
   // _contectLabel.textType = [NSNumber numberWithInteger:LabelColorGray];
    _likeLabel.text = [NSString stringWithFormat:@"有%@人喜欢", tingList.like];
    _playImageView.image = [UIImage imageNamed:@"GZbofang"];
    
}
@end
